/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package facade;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import remoteEntity.Download;

/**
 *  Interacts with downloads table.
 *  Uses native mysql queries.
 * @author vho
 */
@Stateless
public class DownloadFacade extends AbstractFacade<Download> {
    @PersistenceContext(unitName = "filecityeePU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public DownloadFacade() {
        super(Download.class);
    }
    
    public void logDownload(int userId, int downloadId) {
        String query = "insert into download values (null,NOW(), "+downloadId+","+userId+");";
        em.createNativeQuery(query).executeUpdate();
    }
    
    public List<Download> getDownloadsForUser(int id) {
        String query = "SELECT * FROM download WHERE user_id="+id+";";
        return em.createNativeQuery(query,Download.class).getResultList();
    }
}
